package com.mach.platform.repository;

import com.mach.platform.domain.iot.IotSpace;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author Anonymous
 * @Description: 设备管理
 * @Package: com.mach.platform.repository
 * @time 2018/04/27 23:12
 */
@Repository
public interface IotSpaceRepo extends BaseRepository<IotSpace>{

	/**
	 * 查询工厂下面母排侧温下面的所有车间
	 * @param id 工厂空间id
	 * @return
	 */
	@Query(value = "select id value, name label from iot_space where parent_id = (select id from iot_space where parent_id = ?1 and type = 12)", nativeQuery = true)
	List<Map> findBusbarWorkShop(Long id);

	@Query(value = "select id, name from iot_space where parent_id = ?1", nativeQuery = true)
	List<Map> findByParentId(Long id);

	/**
	 * 查询空间的上上级
	 * @param id 空间id
	 * @return
	 */
	@Query(value = "select * from iot_space where id = (select parent_id from iot_space where id = (select parent_id from iot_space where id = ?1))", nativeQuery = true)
	IotSpace findUpperLevel(Long id);

	/**
	 *  获取顶级 空间的 实体数据  规定最顶级空间必须partentId 为0
	 * @return
	 */
	@Query(value = "select * from iot_space  where  parent_id=0", nativeQuery = true)
	IotSpace loadTopSpace ();

	/**
	 * 查询关注设备组下面的全部机组设备id
	 * @param parentIds
	 * @return
	 */
	@Query(value = "select id unitId, parent_id groupId from iot_space where parent_id in (?1) and type = 11", nativeQuery = true)
	List<Map> findByParentIds(List<Long> parentIds);

	/**
	 * 通过父级id和type查询空间
	 * @return
	 */
	IotSpace findFirstByParentIdAndType(Long parendId, int type);

	/**
	 * 修改 空间实体的一个字段
	 * @param spaceId
	 * @param picUrl
	 */
	@Modifying
	@Query(value = "update iot_space set layout_pic = ?2 where id=?1",nativeQuery = true)
	void updateLayoutPicUrl (Long spaceId, String picUrl);

	/**
	 * 加载站房的选项数据
	 * @param spaceId
	 * @param type
	 * @return
	 */
	@Query(value = "select id as value, name as label from iot_space where parent_id = ?1 and type = ?2 order by sort",nativeQuery = true)
	List<Map> loadStationSelectionDatas (Integer spaceId, Integer type);

	@Query(value = "select * from iot_space where parent_id=?1 order by sort",nativeQuery = true)
	List<IotSpace> loadStationsObjsByPid (Long spaceId);

	@Query(value = "select * from iot_space where parent_id=?1 and type = ?2 order by sort",nativeQuery = true)
	List<IotSpace> loadSpaceByGrandaByType (Long spaceId,Integer type);


}
